<template>
  <div>
    <!-- 搜索栏 -->
    <el-input
        v-model="searchText"
        placeholder="姓名"
        class="search-input"
        clearable
    />
    <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
    <el-button type="success" icon="el-icon-edit" @click="handleAdd">添加</el-button>
    <el-button type="primary" icon="el-icon-download" @click="handleExport">导出</el-button>

    <!-- 表格 -->
    <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
      <el-table-column prop="teacherId" label="教师编号" sortable></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="email" label="注册邮箱"></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
        background
        layout="total, sizes, prev, pager, next"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchText: '',
      loading: false,
      tableData: [], // 表格数据
      total: 0, // 数据总条数
      pageSize: 10, // 每页条数
      currentPage: 1, // 当前页码
    };
  },
  methods: {
    // 搜索
    handleSearch() {
      this.fetchTableData();
    },

    // 添加
    handleAdd() {
      // 这里可以打开添加对话框
      this.$message("添加功能待实现");
    },

    // 导出
    handleExport() {
      this.$message("导出功能待实现");
    },

    // 编辑
    handleEdit(row) {
      // 这里可以打开编辑对话框并传入当前行数据
      console.log("编辑", row);
      this.$message(`编辑功能待实现：${row.name}`);
    },

    // 删除
    handleDelete(row) {
      this.$confirm(`确定删除 ${row.name} 吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // 执行删除逻辑
        this.$message({
          type: "success",
          message: "删除成功!",
        });
        this.fetchTableData();
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除",
        });
      });
    },

    // 获取表格数据
    fetchTableData() {
      this.loading = true;
      // 模拟请求数据
      setTimeout(() => {
        // 模拟数据
        this.tableData = [
          {teacherId: '202105006', name: '彭娟', email: '4442331@qq.com'},
          {teacherId: '202105014', name: '刘鹏飞', email: '12321111@qq.com'},
          {teacherId: '202105018', name: '刘风', email: 'albert.b@qq.com'},
          {teacherId: '2024', name: '刘洪', email: 'albert.b@qq.com'}
        ];
        this.total = this.tableData.length;
        this.loading = false;
      }, 1000);
    },

    // 每页条数改变
    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchTableData();
    },

    // 当前页码改变
    handleCurrentChange(page) {
      this.currentPage = page;
      this.fetchTableData();
    },
  },
  mounted() {
    // 初次加载数据
    this.fetchTableData();
  },
};
</script>

<style scoped>
.search-input {
  width: 200px;
  margin-right: 10px;
}
</style>
